/**
 * Created by lxh on 2018/1/3.
 */
import React, {Component} from 'react';
import {connect} from 'dva';
import {Link} from 'dva/router';
import {Form, Card, Row, Col,Input, Select, Table, Button,Radio,Modal} from 'antd';
import styles from './Shop/ShopManage.less';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import {routerRedux} from 'dva/router';
import PowerList from '../../components/AdminPowerEdit/powerList/PowerList';
import SellerList from '../../components/AdminPowerEdit/sellerList/SellerList';
import {powerControl} from '../../utils/powerControl';
const AuthButton = powerControl(Button);
const FormItem = Form.Item;
const {Option} = Select;
@connect(state=>({
    adminPowerSetting: state.adminPowerSetting,
}))
@Form.create()
export default class ManageSeller extends Component {
    state={
        userId:'',
        sellerId:'',//商店id
        searchValues:{},
        modalVisible:false,
        selectedRows:[],
        functionType:'GLYSJGLQXPZ',
    }
    componentDidMount(){
        if(!this.props.location.state){
            this.props.dispatch(routerRedux.push("/management/AdminPowerSetting"))
            return;
        }
        this.props.dispatch({
            type: 'adminPowerSetting/queryAllSeller',
            payload:{userId:this.props.location.state}
        })
        //管理员id
        this.setState({
            userId:this.props.location.state,
        })
    }
    handleSelectRows = (rows) => {
        this.setState({
            selectedRows: rows,
        });
    }
    handleTableChange = (pagination) => {
        const {dispatch} = this.props;
        const {searchValues} = this.state;
        const params = {
            currentPage: pagination.current,
            pageSize: pagination.pageSize,
            ...searchValues,
        };
        dispatch({
            type: 'adminPowerSetting/queryAllSeller',
            payload: params,
        });
    }
    //查商家所有可选择的权限
    handlePower =(record)=>{
        this.handleCancel(true);
        const {userId,functionType}=this.state;
        this.props.dispatch({
            type:'adminPowerSetting/queryPowerList',
            payload:{actionType:1,//商家权限类型
                userId: userId,
                sellerId: record.id,
                functionType: functionType,
            },
            callback:(response)=>{
                if(response.result===0){
                    this.setState({
                        //已经获得的权限
                        selectedRows:this.props.adminPowerSetting.actionList,
                        sellerId:record.id,
                    })
                }
            }
        });
    };

    handleCancel=(flag)=>{
        this.setState({
            modalVisible:!!flag,
        })
    };

    render() {
        const {adminPowerSetting:{sellerData,}}=this.props;
        const {modalVisible,selectedRows,userId ,sellerId,functionType}=this.state;
        return (
            <PageHeaderLayout title="管理员可管理商家">
                <Card bordered={false}>
                    <div className={styles.tableList}>
                        <div className={styles.tableListForm}>
                            <div style={{margin:10,border:0,padding:0}}>
                                <Button type="primary"><Link to={{pathname:'/management/AdminPowerSetting',state:userId}}>返回</Link></Button>
                                <AuthButton auth="adminManageSeller"><Link to={{pathname:'/management/managementAddSeller',state:this.state.userId}}>修改可管理商家</Link></AuthButton>
                            </div>
                            <div>
                                <SellerList
                                data={sellerData}
                                handleTableChange={this.handleTableChange}
                                handlePower={this.handlePower}/>
                            </div>
                            <Modal
                                visible={modalVisible}
                                title="商家权限配置"
                                onCancel={()=>this.handleCancel()}
                                footer={null}>
                                <PowerList
                                    selectedRows={selectedRows}
                                    onSelectRow={this.handleSelectRows}
                                     userId={userId}
                                     sellerId={sellerId}
                                    functionType={functionType}
                                    handleCancel={()=>this.handleCancel()}/>
                            </Modal>
                        </div>
                    </div>
                </Card>
            </PageHeaderLayout>
        )
    }
}